local intramod = the_end_game_master.intramod
local S = minetest.get_translator("the_end_game_master")
local storage = intramod.storage




local start_time = os.clock() - storage:get_float("start_time")




function the_end_game_master.get_running_time()
	if not the_end_game_master.get_stage() == "game"
	then
		return 0
	else
		return os.clock() - start_time
	end
end



local function clear_hud(player)
	local name = player:get_player_name()
	local hud = intramod.huds[name]
	if not hud
	then
		return
	end
	for i, h in ipairs(hud)
	do
		player:hud_remove(h)
	end
	intramod.huds[name] = nil
end

local function show_the_end_logo_on_screen(player)
	local h = player:hud_add(
		{
			hud_elem_type = "image",
			position = {x = 0.5, y = 0.5},
			scale = {x = -90, y = -35},
			name = "game_started_logo",
			text = "the_end_game_master_header.png",
		})
	local name = player:get_player_name()
	local hud = intramod.huds[name]

	if not hud
	then
		hud = {}
		intramod.huds[name] = hud
	end

	table.insert(hud, h)
	minetest.after(3, function()
			if player
			then
				clear_hud(player)
			end
		end)
end

function the_end_game_master.remove_pregame_hud(player)
	local hud_flags = player:hud_set_flags(
		{
			hotbar = true,
			healthbar = true,
			crosshair = true,
			wielditem = true,
			breathbar = true,
		})
	clear_hud(player)
end

local function restore_inv_list_sizes(player)
	local inv = player:get_inventory()
	inv:set_size("main", 4*8)
	inv:set_list("hand", {ItemStack("wieldhand")})
end



local function reenable_sfinv(player)
	minetest.close_formspec(player:get_player_name(), "")
	sfinv.set_player_inventory_formspec(player)
end


local function set_game_physics(player)
	player:set_physics_override(
		{
			speed = 1,
			jump = 1,
			gravity = 1,
			sneak = true,
		})
end

local function set_player_to_game(player)
	the_end_game_master.remove_pregame_hud(player)
	restore_inv_list_sizes(player)
	reenable_sfinv(player)
	set_game_physics(player)
	the_end_game_master.stop_pregame_music(player)
end


local function on_done_loading_map(success)
	if success
	then
		in_game_guide.set_root_page_back_button_name(nil)
		the_end_game_master.clear_mobs()


		the_end_game_master.set_stage("game")
		start_time = os.clock()

		minetest.set_timeofday(0.25) --6am

		mob_spawning_director.reset()
		mob_spawning_director.enable()
		sfinv.enabled = true
		mood_music.start()

		local players = minetest.get_connected_players()
		for _, p in ipairs(players)
		do
			set_player_to_game(p)
			show_the_end_logo_on_screen(p)
			minetest.sound_play("mood_music_transit")
			give_initial_stuff.give(p)
			movable_spawnpoint.spawn_player(p)
		end
	else
		error("Failed loading one of the map's schematics")
	end
end

local function load_random_map()
	local mapnames = {}
	for name, map in pairs(map_master.registered_maps)
	do
		table.insert(mapnames, name)
	end
	assert(#mapnames > 0, "Error starting game: There are no registered maps")
	map_master.load_map(mapnames[math.random(#mapnames)], on_done_loading_map)
end


function the_end_game_master.start_game()
	the_end_game_master.set_stage("loading")

	load_random_map()
end


minetest.register_on_joinplayer(function(player)
		if the_end_game_master.get_stage() == "game"
		then
			set_player_to_game(player)
		end
	end)
